/*
 * @Description:
 * @Author: Zzt
 * @Date: 2022-11-10 16:49:18
 * @LastEditTime: 2022-12-14 09:33:48
 */
import { createContext, useReducer, useEffect, useContext } from 'react';
import { Modal } from 'antd';
import {
  SET_SIDEBAR,
  SET_SELECTED_ITEM,
  SET_ANNUALREP_LIST,
  ADD_REPORT,
  EDIT_REPORT,
  DELETE_REPORT
} from '../constants';
import { AnnualReportReducer } from '../reducers/AnnualReportReducer';
import {
  searchAnnualReportByPage,
  addAnnualReport,
  editAnnualReport,
  deleteAnnualReportList
} from '@/api/statistics/annualReport';
const initialState = {
  selectItems: [],
  annualList: [{ id: 1 }],
  sidebarData: 'all'
};
const AnnualReportContext = createContext();
const AnnualReportContextProvider = (props) => {
  const [state, dispatch] = useReducer(AnnualReportReducer, initialState);
  const actions = {
    /* 选择左列表 */
    changeSidebar(data) {
      dispatch({ type: SET_SIDEBAR, data });
    },
    /**
     * @description: 设置表格中选中条目的key
     * @param {*} selectArr
     * @return {*}
     */
    setSelectKey(selectArr) {
      dispatch({ type: SET_SELECTED_ITEM, data: selectArr });
    },
    /**
     * @description: 请求档案分类列表
     * @param {object} params 请求参数
     * @return {*}
     */
    getAnnualReportList(params) {
      return new Promise((resolve, reject) => {
        searchAnnualReportByPage(params)
          .then((res) => {
            const { data } = res;
            dispatch({ type: SET_ANNUALREP_LIST, data });
            resolve(data);
          })
          .catch((err) => {
            dispatch({ type: SET_ANNUALREP_LIST, data: [] });
            reject(err);
          });
      });
    },
    /* 增加报表 */
    addReportList(params) {
      return new Promise((resolve, reject) => {
        addAnnualReport(params)
          .then((res) => {
            const { data } = res;
            dispatch({ type: ADD_REPORT, data });
            resolve(data);
          })
          .catch((err) => reject(err));
      });
    },
    /* 修改报表 */
    editReportList(params) {
      return new Promise((resolve, reject) => {
        editAnnualReport(params)
          .then((res) => {
            const { data } = res;
            dispatch({ type: EDIT_REPORT, data });
            resolve(data);
          })
          .catch((err) => reject(err));
      });
    },
    /**
     * @description: 删除
     * @param {*} id 删除的id
     * @param {*} content 弹窗展示内容
     * @return {*}
     */
    deleteReportFunc(id, content = '确认删除该年报？') {
      Modal.confirm({
        title: '删除',
        content,
        onOk() {
          const ids = id instanceof Array ? id.join(',') : id;
          deleteAnnualReportList(ids).then((res) => {
            const data = res.data.map((id) => parseInt(id, 10));
            dispatch({ type: DELETE_REPORT, ids: data });
          });
        },
        onCancel() {
          console.log('Cancel');
        }
      });
    }
  };
  useEffect(() => {}, []);
  return (
    <AnnualReportContext.Provider value={{ ...state, ...actions }}>
      {props.children}
    </AnnualReportContext.Provider>
  );
};
export const useAnnualReportContext = () => {
  return useContext(AnnualReportContext);
};
export { AnnualReportContext, AnnualReportContextProvider };
